package com.pride1952.myoptimusx3;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;


public class EditCardActivity extends Activity {

    private ImageButton mImgBtnPhoto;
    private Button mBtnFinish;
    private EditText mEditName;
    private EditText mEditStuNum;
    private EditText mEditNumber;
    private EditText mEditMail;
    private EditText mEditHobby;
    private TitleFragment mTitle;

    public BluetoothLeService mBluetoothLeService;
    // Code to manage Service lifecycle.
    private final ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Toast.makeText(getApplicationContext(),"连接中断",Toast.LENGTH_SHORT).show();
            mBluetoothLeService = null;
        }
    };


    private IntentFilter intentFilter(){
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(BluetoothLeService.mACTION_IDCARD_WRITE);
        return intentFilter;
    }
    private BroadcastReceiver bcReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action)
            {
                case BluetoothLeService.mACTION_IDCARD_WRITE:
                    Toast.makeText(getApplicationContext(), "名片写入成功", Toast.LENGTH_SHORT).show();
                    SharedPreferences mPreferences=getSharedPreferences("MyCard",MODE_PRIVATE);
                    SharedPreferences.Editor mEditor=mPreferences.edit();
                    mEditor.putString("Name",mEditName.getText().toString());
                    mEditor.putString("StuNum", mEditStuNum.getText().toString());
                    mEditor.putString("Number",mEditNumber.getText().toString());
                    mEditor.putString("Mail", mEditMail.getText().toString());
                    mEditor.putString("Hobby", mEditHobby.getText().toString());
                    mEditor.apply();
                    finish();
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
        bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
        registerReceiver(bcReceiver, intentFilter());

        setContentView(R.layout.activity_edit_card);
        initTitle();
        initWidgets();
    }
    private void initTitle(){
        FragmentManager fm=getFragmentManager();
        FragmentTransaction transaction=fm.beginTransaction();
        Bundle bundle=new Bundle();
        bundle.putString("Title","编辑名片");
        bundle.putBoolean("IsBack",true);
        mTitle=new TitleFragment();
        mTitle.setArguments(bundle);
        transaction.replace(R.id.title_edit_card, mTitle);
        transaction.commit();
    }
    void initWidgets(){
        mBtnFinish=(Button)findViewById(R.id.btn_edit_finish);
        mImgBtnPhoto=(ImageButton)findViewById(R.id.imgbtn_edit_photo);
        mEditName=(EditText)findViewById(R.id.edit_edit_name);
        mEditStuNum =(EditText)findViewById(R.id.edit_edit_student_number);
        mEditNumber=(EditText)findViewById(R.id.edit_edit_number);
        mEditMail=(EditText)findViewById(R.id.edit_edit_mail);
        mEditHobby=(EditText)findViewById(R.id.edit_edit_hobby);

        SharedPreferences mPreferences=getSharedPreferences("MyCard", MODE_PRIVATE);
        mEditName.setText(mPreferences.getString("Name", ""));
        mEditStuNum.setText(mPreferences.getString("StuNum", ""));
        mEditStuNum.setInputType(InputType.TYPE_CLASS_TEXT);
        mEditNumber.setText(mPreferences.getString("Number", ""));
        mEditNumber.setInputType(InputType.TYPE_CLASS_PHONE);
        mEditMail.setText(mPreferences.getString("Mail", ""));
        mEditMail.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        mEditHobby.setText(mPreferences.getString("Hobby", ""));

        mBtnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mBluetoothLeService.isReady()) {
                    mBluetoothLeService.doSendIDcard(createIDCard());
                }
                else {
                    Toast.makeText(getApplicationContext(),"手环未连接或忙碌",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        unregisterReceiver(bcReceiver);
        unbindService(mServiceConnection);
    }

    private IDcard createIDCard()
    {
        IDcard iDcard=new IDcard();
        iDcard.n=mEditName.getText().toString();
        iDcard.s=mEditStuNum.getText().toString();
        iDcard.p=mEditNumber.getText().toString();
        return iDcard;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_edit_card, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
